package cn.iocoder.yudao.module.xiaoBizHandler.controller.admin.tbGenGoodsRegister;

import cn.iocoder.yudao.framework.common.genId.PKGenerator;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;

import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.xiaoBizHandler.controller.admin.tbGenGoodsRegister.vo.*;
import cn.iocoder.yudao.module.xiaoBizHandler.dal.dataobject.tbGenGoodsRegister.XTbGenGoodsRegisterDO;
import cn.iocoder.yudao.module.xiaoBizHandler.convert.tbGenGoodsRegister.XTbGenGoodsRegisterConvert;
import cn.iocoder.yudao.module.xiaoBizHandler.service.tbGenGoodsRegister.XTbGenGoodsRegisterService;

@Api(tags = "管理后台 - 物品登记")
@RestController
@RequestMapping("/xiaoBizHandler/XT-b-gen-goods-register")
@Validated
public class XTbGenGoodsRegisterController {

    @Resource
    private XTbGenGoodsRegisterService xTbGenGoodsRegisterService;

    @PostMapping("/create")
    @ApiOperation("创建物品登记")
    @PreAuthorize("@ss.hasPermission('xbh:XT-b-gen-goods-register:create')")
    public CommonResult<String> createXTbGenGoodsRegister(@Valid @RequestBody XTbGenGoodsRegisterCreateReqVO createReqVO) {
        createReqVO.setId(PKGenerator.newId());
        return success(xTbGenGoodsRegisterService.createXTbGenGoodsRegister(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新物品登记")
    @PreAuthorize("@ss.hasPermission('xbh:XT-b-gen-goods-register:update')")
    public CommonResult<Boolean> updateXTbGenGoodsRegister(@Valid @RequestBody XTbGenGoodsRegisterUpdateReqVO updateReqVO) {
        xTbGenGoodsRegisterService.updateXTbGenGoodsRegister(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除物品登记")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('xbh:XT-b-gen-goods-register:delete')")
    public CommonResult<Boolean> deleteXTbGenGoodsRegister(@RequestParam("id") String id) {
        xTbGenGoodsRegisterService.deleteXTbGenGoodsRegister(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得物品登记")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('xbh:XT-b-gen-goods-register:query')")
    public CommonResult<XTbGenGoodsRegisterRespVO> getXTbGenGoodsRegister(@RequestParam("id") String id) {
        XTbGenGoodsRegisterDO xTbGenGoodsRegister = xTbGenGoodsRegisterService.getXTbGenGoodsRegister(id);
        return success(XTbGenGoodsRegisterConvert.INSTANCE.convert(xTbGenGoodsRegister));
    }

    @GetMapping("/list")
    @ApiOperation("获得物品登记列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('xbh:XT-b-gen-goods-register:query')")
    public CommonResult<List<XTbGenGoodsRegisterRespVO>> getXTbGenGoodsRegisterList(@RequestParam("ids") Collection<String> ids) {
        List<XTbGenGoodsRegisterDO> list = xTbGenGoodsRegisterService.getXTbGenGoodsRegisterList(ids);
        return success(XTbGenGoodsRegisterConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得物品登记分页")
    @PreAuthorize("@ss.hasPermission('xbh:XT-b-gen-goods-register:query')")
    public CommonResult<PageResult<XTbGenGoodsRegisterRespVO>> getXTbGenGoodsRegisterPage(@Valid XTbGenGoodsRegisterPageReqVO pageVO) {
        PageResult<XTbGenGoodsRegisterDO> pageResult = xTbGenGoodsRegisterService.getXTbGenGoodsRegisterPage(pageVO);
        return success(XTbGenGoodsRegisterConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出物品登记 Excel")
    @PreAuthorize("@ss.hasPermission('xbh:XT-b-gen-goods-register:export')")
    @OperateLog(type = EXPORT)
    public void exportXTbGenGoodsRegisterExcel(@Valid XTbGenGoodsRegisterExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<XTbGenGoodsRegisterDO> list = xTbGenGoodsRegisterService.getXTbGenGoodsRegisterList(exportReqVO);
        // 导出 Excel
        List<XTbGenGoodsRegisterExcelVO> datas = XTbGenGoodsRegisterConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "物品登记.xls", "数据", XTbGenGoodsRegisterExcelVO.class, datas);
    }

}
